package c2.cross.server.configuration;

import c2.cross.business.CrossBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

// 通过使用ConfigurableApplicationContext接口上的close（）方法关闭ApplicationContext时发布。
// 在此，“封闭”表示所有单例豆都被破坏。封闭的情境到了生命的尽头。无法刷新或重新启动。
@Slf4j
@Component
public class AppClosedListener implements ApplicationListener<ContextClosedEvent> {

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        log.info("监听 ContextClosedEvent = {}", event);
        CrossBody body = event.getApplicationContext().getBean(CrossBody.class);
        log.info("所有模块shutdown开始");
        long beginTime = System.currentTimeMillis();
        body.shutdownModules();
        log.info("所有模块shutdown完成 耗时{}ms", (System.currentTimeMillis() - beginTime));
    }
}
